import java.util.HashMap;
import java.util.Map;

/**
 * @USER: Administrator
 * @DATE: 2024/9/17 下午6:58
 * @VERSION: 1.0.0
 */
public class LC819 {

    public static void main(String[] args) {
        LC819 lc819 = new LC819();
        String p = "Bob. hIt, baLl";
        String[] banned = new String[]{"bob", "hit"};
        System.out.println(lc819.mostCommonWord(p,banned));
    }

    public String mostCommonWord(String paragraph, String[] banned) {
        StringBuilder word = new StringBuilder();
        paragraph = paragraph.toLowerCase();
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < paragraph.length(); i++) {
            if (paragraph.charAt(i)>='a' && paragraph.charAt(i)<='z') {
                word.append(paragraph.charAt(i));
                if (i == paragraph.length()-1){
                    map.put(word.toString(), map.getOrDefault(word.toString(), 0) + 1);
                }
            }else {
                if (!contains(banned,word.toString())&& !"".contentEquals(word)){
                    map.put(word.toString(), map.getOrDefault(word.toString(), 0) + 1);
                }
                word = new StringBuilder();
            }
        }

        String key = "";
        int max = 0;
        for (String s : map.keySet()) {
            if (map.get(s) > max) {
                max = map.get(s);
                key = s;
            }
        }

        return key;
    }

    public boolean contains(String[] banned, String word){
        if(banned == null || banned.length == 0) return false;
        if(word == null || word.isEmpty()) return false;
        for (String s : banned) {
            if (s.equals(word)) return true;
        }
        return false;
    }

}
